// 小程序反馈
import { DataTypes, UUIDV4 } from 'sequelize';
import { MySqlModel, getSequelize } from '../db';
import { getToday } from '../utils';

class MiniTicket extends MySqlModel {

}

MiniTicket.init({
  id: {
    type: DataTypes.UUID, unique: 'id', allowNull: false, defaultValue: UUIDV4, validate: { isUUID: 4 }, primaryKey: true, comment: '主键'
  },
  hotel_id: {
    type: DataTypes.UUID, allowNull: false, validate: { isUUID: 4 }, comment: '主体'
  },
  openid: { type: DataTypes.STRING(50), allowNull: true, comment: '用户唯一标识' },
  ip_address: {
    type: DataTypes.STRING(100), allowNull: true, comment: '客户端IP地址'
  },
  business_date: {
    type: DataTypes.DATEONLY, allowNull: false, comment: '日期'
  },
  description: {
    type: DataTypes.STRING(200), allowNull: false, comment: '反馈内容'
  },
  mobile: {
    type: DataTypes.STRING(20), allowNull: true, defaultValue: null, comment: '手机号'
  },
  image1: { type: DataTypes.STRING(100), allowNull: true, defaultValue: null, comment: '图片1' },
  image2: { type: DataTypes.STRING(100), allowNull: true, defaultValue: null, comment: '图片2' },
  image3: { type: DataTypes.STRING(100), allowNull: true, defaultValue: null, comment: '图片3' },
  image4: { type: DataTypes.STRING(100), allowNull: true, defaultValue: null, comment: '图片4' },
  status: { type: DataTypes.STRING(20), allowNull: false, validate: { isIn: { args: [['Pending', 'Closed']], msg: '无效的状态' } }, comment: '状态' },
  comment: { type: DataTypes.STRING(200), allowNull: true, defaultValue: null, comment: '处理结果' },
  closed_at: {
    type: DataTypes.DATE, allowNull: true, defaultValue: null, comment: '关单时间'
  },
  updated_id: { type: DataTypes.UUID, allowNull: true, defaultValue: null, validate: { isUUID: 4 }, comment: '处理人' },
  created_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '更新时间'
  },
  version: {
    type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, editable: false, comment: '版本控制'
  }
}, {
  sequelize: getSequelize(),
  tableName: 'mini_ticket',
  comment: '用户反馈记录'
});

MiniTicket.beforeValidate((instance, options) => {
  if (instance.isNewRecord) {
    instance.business_date = getToday();
    instance.status = 'Pending';
  }
})

MiniTicket.associate = (models) => {
  MiniTicket.belongsTo(models.Users, {
    foreignKey: 'updated_id',
    as: 'updater',
    constraints: false
  })
}

export default MiniTicket;
